package cn.itcast.nsfw.complain.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.itcast.core.service.impl.BaseServiceImpl;
import cn.itcast.nsfw.complain.dao.ComplainDao;
import cn.itcast.nsfw.complain.entity.Complain;
import cn.itcast.nsfw.complain.service.ComplainService;

@Service("complainService")
public class ComplainServiceImpl extends BaseServiceImpl<Complain> implements ComplainService {

	private ComplainDao complainDao;
	@Resource
	public void setComplainDao(ComplainDao complainDao) {
		super.setBaseDao(complainDao);
		this.complainDao = complainDao;
	}
	
	@Override
	public void autoDeal() {
		//获取当期月的1号0时0分0秒
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 1);//1号
		cal.set(Calendar.HOUR_OF_DAY, 0);//0时
		cal.set(Calendar.MINUTE, 0);//0分
		cal.set(Calendar.SECOND, 0);//0秒
	
		//将本月之前的待受理的投诉信息的状态改为已失效
		complainDao.updateStateByStateAndBeforeCompTime(cal.getTime(), Complain.COMPLAIN_STATE_UNDONE, Complain.COMPLAIN_STATE_INVALID);
	}

	@Override
	public List getStatisticDataByYear(int year) {
		//1、查询统计数据
		List<Object[]> list = complainDao.getStatisticDataByYear(year);
		List<Map<String, Object>> resList = new ArrayList<Map<String, Object>>();
		Calendar cal = Calendar.getInstance();
		boolean isCurYear = (year == cal.get(Calendar.YEAR));
		int curMonth = cal.get(Calendar.MONTH) + 1;//当前月份
		if(list != null && list.size()>0){
			//2、组装符合要求的返回列表（label / value）
			Map<String, Object> map = null;
			int temMonth = 0;
			for(Object[] obj: list){
				map = new HashMap<String, Object>();
				temMonth = Integer.parseInt(obj[0]+"");//月份
				map.put("label", temMonth + "月");
				if(isCurYear){//当前年度：如果对应的月份还未到的则将投诉数为0或者其它值的设置为空字符串
					if(temMonth > curMonth){//月份还未到
						map.put("value", "");
					} else {//月份已过，把投诉数为空或者null的设置为0
						map.put("value", obj[1]!=null?obj[1]:"0");
					}
				} else {//非当前年度：把投诉数为空或者null的设置为0
					map.put("value", obj[1]!=null?obj[1]:"0");
				}
				resList.add(map);
			}
		}
		return resList;
	}

}
